package io.github.wppli.infrastructure.persistent.repository;

import io.github.wppli.domain.posts.model.valobj.UserPostsBehaviorType;
import io.github.wppli.domain.recommond.model.entity.UserPostsBehaviorsEntity;
import io.github.wppli.domain.recommond.repository.IUserPostsBehaviorRepository;
import io.github.wppli.infrastructure.persistent.dao.IUserPostsBehaviorDao;
import io.github.wppli.infrastructure.persistent.po.UserPostsBehavior;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author li--jiaqiang 2025−03−15
 */
@Repository
@AllArgsConstructor
public class UserPostsBehaviorRepository implements IUserPostsBehaviorRepository {

    private final IUserPostsBehaviorDao userPostsBehaviorDao;

    @Override
    public List<UserPostsBehaviorsEntity> queryByUserId(Long userId) {
        List<UserPostsBehavior> behaviors = userPostsBehaviorDao.queryByUserId(userId);
        if (behaviors == null || behaviors.isEmpty()) {
            return Collections.emptyList();
        }
        return behaviors.stream().map(
                userPostsBehavior -> UserPostsBehaviorsEntity.builder()
                        .userId(userPostsBehavior.getUserId())
                        .postsContentId(userPostsBehavior.getPostsId())
                        .build()
        ).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void save(Long userId, Long contentId, UserPostsBehaviorType userPostsBehaviorType) {
        UserPostsBehavior userPostsBehavior = new UserPostsBehavior();
        userPostsBehavior.setUserId(userId);
        userPostsBehavior.setPostsId(contentId);
        userPostsBehavior.setBehaviorType(userPostsBehavior.getBehaviorType());
        userPostsBehavior.setScore(userPostsBehaviorType.getScore());

        userPostsBehaviorDao.insert(userPostsBehavior);
    }
}